#include "student.h"

int main()
{
    system("chcp 65001");
    if (toujiedian() != 0)
    {
        printf("\n\t为头结点分配空间失败！");
        return 0;
    }
    char ch, sh;
    while (1)
    {
        system("color E0"); //改变控制台的背景色
        selectOption();
        ch = _getch();
        switch (ch)
        {
        case '1': //管理员登录
            if (denglu() == 0)
            {
                printf("\n\t登陆成功");
                _getch();
                system("cls");
                do
                {
                    system("color C0");
                    admin_menu();
                    sh = _getch();
                    switch (sh)
                    {
                    case '1': //售票
                        if (shoupiao() == 0)
                        {
                            _getch();
                            system("cls");
                        }
                        else
                        {
                            printf("\n\t售票失败");
                            _getch();
                            system("cls");
                        }
                        break;
                    case '2': //退票
                        if (tuipiao() == 0)
                        {
                            _getch();
                            system("cls");
                        }
                        else
                        {
                            printf("\n\t退票失败");
                            _getch();
                            system("cls");
                        }
                        break;
                    case '3': //改签
                        if (gaiqian() == 0)
                        {
                            _getch();
                            system("cls");
                        }
                        else
                        {
                            printf("\n\t改签失败");
                            _getch();
                            system("cls");
                        }
                        break;
                    case '4': //列车车次管理
                        railwaymanage();
                        break;
                    case '5':
                        chengkemanage();
                        _getch();
                        system("cls");
                        break;
                    default:
                        printf("Please enter a right option number!!\n");
                        break;
                    }

                } while (sh != '6');
                system("cls");
            }
            else
            {
                printf("\n\t操作失败");
                _getch();
                system("cls");
            }
            break;
        case '2':
            if (railway_see() == 0)
            {
                printf("\n\t操作成功");
                _getch();
                system("cls");
            }
            else
            {
                printf("\n\t操作失败");
                _getch();
                system("cls");
            }

            break;
        default:
            printf("\n\tPlease enter a right option number!");
            break;
        }
    }
    while (ch != '0')
        ;

    printf("\n\tquit");

    return 0;
}

int toujiedian()
{
    r_head = (railway *)malloc(sizeof(railway));
    c_head = (customer *)malloc(sizeof(customer));
    if (r_head == NULL || c_head == NULL)
        return -1;
    else
    {
        r_head->next = NULL;
        c_head->next = NULL;
        return 0;
    }
}

void selectOption(void)
{
    printf("*****************************************************************\n");
    printf("*\t\t\t火车票售票系统\t\t\t\t*\n");
    printf("*****************************************************************\n");
    printf("*\t\t\t请选择功能列表\t\t\t\t*\n");
    printf("*****************************************************************\n");
    printf("*\t\t\t1.管理员登陆\t\t\t\t*\n");
    printf("*\t\t\t2.查看票剩余信息\t\t\t*\n");
    printf("*\t\t\t0.Quit the System\t\t\t*\n");
    printf("*****************************************************************\n");
}

int denglu()
{
    // char name[50];
    // char pwd[10];
    // int i = 0;
    // printf("\t请输入用户名:");
    // scanf("%s", &name);
    // printf("\t请输入密码:");
    // for (i = 0; i < 10; i++)
    // {
    //     pwd[i] = _getch();
    //     if (pwd[i] == '\r')
    //     {
    //         break;
    //     }
    //     printf("*");
    // }
    // pwd[i] = '\0';
    // FILE *fp; //指向文件的指针
    // char tab;
    // int flag = 0;
    // if ((fp = fopen(pwdfile, "r")) != NULL) //打开文件
    // {
    //     int s = fseek(fp, 0, SEEK_END);
    //     int e = ftell(fp);
    //     fseek(fp, 0, SEEK_SET);
    //     if (s == e)
    //     {
    //         printf("\t文件内容为空\n");
    //         return -1;
    //         // return 0;
    //     }

    //     while (!feof(fp)) //判断是否到文件末尾
    //     {
    //         struct Admin admin;
    //         fscanf(fp, "%s", &admin.name);
    //         fscanf(fp, "%c", &tab);
    //         fscanf(fp, "%s", &admin.pwd);

    //         if (strcmp(admin.name, name) == 0 && strcmp(admin.pwd, pwd) == 0)
    //         {
    //             flag = 1;
    //             break;
    //         }
    //     }
    // }
    // else
    // {
    //     printf("\t打开失败\n");
    //     fclose(fp);
    //     return -1;
    // }
    // fclose(fp);
    // if (flag == 1)
    // {
    //     return 0;
    // }
    // else
    // {
    //     return -1;
    // }
    return 0;
}

void admin_menu()
{
    printf("*********************************************************\n");
    printf("*\t\t\t火车票务 管理系统\t\t*\n");
    printf("*********************************************************\n");
    printf("*\t\t\t请选择功能列表\t\t\t*\n");
    printf("*********************************************************\n");
    printf("*\t\t\t1.售    票\t\t\t*\n");
    printf("*\t\t\t2.退    票\t\t\t*\n");
    printf("*\t\t\t3.改    签\t\t\t*\n");
    printf("*\t\t\t4.列车 车次 管理\t\t*\n");
    printf("*\t\t\t5.乘客 信息 管理\t\t*\n");
    printf("*\t\t\t6.返回上一级菜单\t\t*\n");
    printf("*********************************************************\n");
}

char Continue() //询问是否继续函数
{
    char answer;
    while (1)
    {
        printf("\n\t您是否想继续（Y/N）?\n");
        scanf("%s", &answer);
        if (answer == 'y' || answer == 'Y')
            return 'y';
        else if (answer == 'n' || answer == 'N')
            return 'n';
        else
            printf("\n\t输入有误，请重新输入！");
    }
}

int railway_see()
{
    FILE *fp = NULL;

    struct railway railway;
    char tab;

    fp = fopen("railway.txt", "r");
    if (!fp)
    {
        printf("\n\t文件不存在\n");
        return -1;
    }
    while (!feof(fp))
    {

        fscanf(fp, "%s", &railway.time);
        fscanf(fp, "%c", &tab);
        fscanf(fp, "%s", &railway.railway_no);
        fscanf(fp, "%c", &tab);
        fscanf(fp, "%s", &railway.price[0]);
        fscanf(fp, "%c", &tab);
        fscanf(fp, "%d", &railway.price[1]);
        fscanf(fp, "%c", &tab);
        fscanf(fp, "%d", &railway.price[2]);
        fscanf(fp, "%c", &tab);
        printf("\n\t列车出发结束时间：%d\t列车车次：%d\t硬座价钱：%d\t硬卧价钱：%d\t软卧价钱：%d", &railway.time, railway.railway_no, &railway.price[0], &railway.price[1], &railway.price[2]);
    }
    fclose(fp);
    return 0;
}

railway *find_railway(int answer) //列车号查找函数
{

    railway pfind = r_head->next;
    if (pfind == NULL)
    {
        printf("链表为空\n");
    }

    while (ptr != NULL)
    {
        printf("%d ", pfind->key);
        ptr = ptr->next;
    }

    return NULL;
}

int shoupiao()
{
    int id[50];     //证件号
    int answer[50]; //列车号
    customer *pnew = NULL;
    customer *pfind = c_head;
    railway *p;
    while (pfind->next != NULL)
    {
        pfind = pfind->next;
    }
    pnew = (customer *)malloc(sizeof(customer));
    char c;
    printf("\n\t现在您可以购票了！\n");
    printf("\n\t请输入列车号（10位）：");
    scanf("%s", &answer);
    p = find_railway(answer);
    if (p != NULL)
    {
        printf("\n\t请输入购票人的id信息（10位）：");
        scanf("%d", &pnew->id);
        printf("\n\t一个id只限购一张票！\n");
        /*if (p1->free_a >= 1)
        {
        }
        else if (p1->free_a == 0)
        {
            printf("\n\t对不起，票已售完！");
            return -1;
        }
        else
        {
            printf("\n\t对不起，当前没有多余的票！");
            return -1;
        }*/
        printf("\n\t请输入购票人的姓名：");
        scanf("%s", pnew->name);
        printf("\n\t请输入数字选择票种：");
        printf("\n\t1、硬座价格：X\n\t2、硬卧价格：2*X+Y\n\t3、软卧价格：3X+Z*X+Z");
        printf("\n\t请选择：");
        scanf("%d", &pnew->rank);
        printf("\t输入成功！");
        strcpy(pnew->railway_no, answer);
        printf("\n\t恭喜您订票成功！");
        pnew->next = NULL;
        pfind->next = pnew;
        return 0;
    }
    else
    {
        printf("\n\t对不起，没有此列车！");
    }
    //printf("\n\t是否退出菜单？：（Y/N）");
    //scanf("%s", c);
    //if (c == 'y' || c == 'Y')
    return 0;
}

int tuipiao()
{
    customer *ptemp = NULL;
    customer *target = NULL;
    customer *pfind = c_head;
    railway *p;
    int answer[50]; //列车车次
    char pname[50]; //乘客姓名
    int pid[50];    //乘客id
    printf("\n\t请输入乘客的车票车次：");
    scanf("%s", &answer);
    printf("\n\t请输入乘客姓名：");
    scanf("%s", pname);
    printf("\n\t请输入乘客id：");
    scanf("%d", &pid);
    p = find_railway(answer);
    if (p != NULL)
    {
        printf("\n\t现在您可以退票了！\n");
        while (pfind != NULL)
        {
            if (strcmp(pfind->name, pname) == 0 || pfind->id == pid)
            {
                target == pfind;
                if (target == NULL)
                {
                    return -1;
                }
                else
                {
                    while (pfind->next != target)
                    {
                        pfind = pfind->next;
                    }
                    free(target);
                    target = NULL;
                    pfind->next = ptemp;
                    printf("\n\t退票成功！");
                    return 0;
                }
            }
            pfind = pfind->next;
        }
        return -1;
    }
    else
    {
        printf("\n\t对不起，没有此列车！");
        return -1;
    }
}

int gaiqian()
{
    customer *target = NULL;
    customer *pfind = c_head;
    railway *p;
    int answer[50]; //列车车次
    char pname[50]; //乘客姓名
    int pid[50];    //乘客id
    int a;
    int rank;
    printf("\n\t请输入乘客的车票车次：");
    scanf("%s", answer);
    printf("\n\t请输入乘客姓名：");
    scanf("%s", pname);
    printf("\n\t请输入乘客id：");
    scanf("%s", pid);
    p = find_railway(answer);
    if (p != NULL)
    {
        printf("\n\t现在您可以进行改签了！\n");
        while (pfind != NULL)
        {
            if (strcmp(pfind->name, pname) == 0 || pfind->id == pid)
            {
                target == pfind;
                if (target == NULL)
                {
                    return -1;
                }
                else
                {
                    while (pfind->next != target)
                    {
                        pfind = pfind->next;
                    }
                    printf("\n\t请输入数字来确定你的改签内容：");
                    printf("\n\t1、更换票种\n\t2、更换乘车时间");
                    scanf("%d", &a);
                    if (a == 1)
                    {
                        printf("\n\t请输入数字选择票种：");
                        printf("\n\t1、硬座价格：X\n\t2、硬卧价格：2*X+Y\n\t3、软卧价格：3*X+Z");
                        printf("\n\t请选择：");
                        scanf("%d", &target->rank);
                        printf("\t输入成功！");
                        printf("\n\t恭喜您改签成功！");
                    }
                    else
                    {
                        printf("\n\t请输入更改后的车次");
                        scanf("%s", &target->railway_no);
                        printf("\n\t恭喜您改签成功！");
                    }
                    return 0;
                }
            }
            pfind = pfind->next;
        }
        return -1;
    }
    else
    {
        printf("\n\t对不起，没有此列车！");
        return -1;
    }
}

int railway_add()
{
    // railway *pfind = r_head;
    // railway *p1 = NULL;
    // while (pfind->next != NULL)
    // {
    //     pfind = pfind->next;
    // }
    // p1 = (railway *)malloc(sizeof(railway));
    // if (p1 == NULL)
    // {
    //     printf("内存不足\n");
    //     return -1;
    // }
    // printf("\n\t添加新的列车号\n");
    // printf("\n\t请输入列车号：");
    // scanf("%d", &p1->railway_no);
    // printf("\n\t请输入列车出发和结束时间：");
    // scanf("%d", &p1->railway_no);
    // printf("\n\t请输入硬座价格：");
    // scanf("%d", &p1->price[0]);
    // printf("\n\t请输入硬卧价格：");
    // scanf("%d", &p1->price[1]);
    // printf("\n\t请输入软卧价格：");
    // scanf("%d", &p1->price[2]);
    // printf("\n\t列车车次录入成功！\n");
    // p1->next = NULL;
    // pfind->next = p1;
    //p1->next = pfind->next;pfind->next = p1;
    //if (Continue() == 'n')
    // return 0;
    createRailway_add();
}

createRailway_add()
{

    // scanf("%d", a);
    railway *p1;
    p1 = (railway *)malloc(sizeof(railway));
    (*p1).railway_no = 10;

    // if (r_head == 0)
    // {
    *p1 = *r_head->next;
    // }
    // r_head->next = 20;
    return 0;
}

void saverailway()
{

    FILE *fp = fopen(filename2, "w"); //文件指针，以写的方式
    if (!fp)                          //文件指针出现错误
    {
        printf("\n\t文件操作失败");
        system("pause");
        system("cls");
        return;
    }
    railway *p = r_head;
    if (r_head == NULL)
    {
        printf("\n\t链表为空，保存失败");
        system("pause");
        system("cls");
        return;
    }
    p = p->next;
    while (p->next != NULL)
    {
        //换行
        int s = ftell(fp);
        if (s != 0)
        {
            fprintf(fp, "%c", '\n');
        }
        //写入文件

        fprintf(fp, "%s", p->time);
        fprintf(fp, "%c", '\t');
        fprintf(fp, "%s", p->railway_no);
        fprintf(fp, "%c", '\t');
        fprintf(fp, "%d", p->price[0]);
        fprintf(fp, "%c", '\t');
        fprintf(fp, "%d", p->price[1]);
        fprintf(fp, "%c", '\t');
        fprintf(fp, "%d", p->price[2]);
        p = p->next;
    }
    fprintf(fp, "%c", '\n'); //写入最后一个结点
    if (p != NULL)
    {
        //写入文件

        fprintf(fp, "%s", p->time);
        fprintf(fp, "%c", '\t');
        fprintf(fp, "%s", p->railway_no);
        fprintf(fp, "%c", '\t');
        fprintf(fp, "%d", p->price[0]);
        fprintf(fp, "%c", '\t');
        fprintf(fp, "%d", p->price[1]);
        fprintf(fp, "%c", '\t');
        fprintf(fp, "%d", p->price[2]);
    }
    fclose(fp);
    printf("\n\t保存成功！");
    system("pause");
    system("cls");
    return;
}

void railwaymanage()
{
    char c;
    system("color B0");
    system("cls");
    while (1)
    {
        printf("\n\t\t 列车车次管理 \n");
        printf("*****************************************\n");
        printf("\t\t1.添加 列车 车次\n");
        printf("\t\t2.保存 车次 信息\n");
        printf("\t\t3.返 回 上 一 级\n");
        printf("*****************************************\n");
        printf("\t请选择您要的服务：");
        c = getch();
        switch (c)
        {
        case '1':
            if (railway_add() == 0)
            {

                printf("\n\t操作成功");
                _getch();
                system("cls");
            }
            else
            {
                printf("\n\t操作失败");
                _getch();
                system("cls");
            }
            break;
        case '2':
            saverailway();
            break;
        case '3':
            printf("\n\t输入成功！");
            _getch();
            system("cls");
            return;
        default:
            printf("Please enter a right option number!!\n");
            break;
        }
    }
}

void checkchengke()
{
    customer *ptemp = NULL;
    customer *target = NULL;
    customer *pfind = c_head;
    char pname[50]; //乘客姓名
    int pid[50];    //乘客id
    printf("\n\t请输入乘客姓名：");
    scanf("%s", pname);
    printf("\n\t请输入乘客id：");
    scanf("%s", pid);
    printf("\n\t开始查找信息:\n");
    while (pfind != NULL)
    {
        if (strcmp(pfind->name, pname) == 0 || strcmp(pfind->id, pid) == 0)
        {
            target == pfind;
            if (target == NULL)
            {
                printf("\n\t查无此人");
                return;
            }
            else
            {
                while (pfind->next != target)
                {
                    pfind = pfind->next;
                }
                printf("\n\t查找成功");
                system("pause");
                printf("\n\t乘客姓名：%s\n乘客id：%d\n票种：%d\n乘客所乘坐列车号：%d", target->name, &target->id, &target->rank, target->railway_no);
                return;
            }
        }
        pfind = pfind->next;
    }
}

int savechengke()
{
    FILE *fp = fopen(filename1, "w"); //文件指针，以写的方式
    if (fp == NULL)                   //文件指针出现错误
    {
        printf("\n\t文件操作失败");
        system("pause");
        system("cls");
        return -1;
    }
    customer *p = c_head;
    if (c_head == NULL)
    {
        printf("\n\t链表为空,保存失败");
        system("pause");
        system("cls");
        return -1;
    }
    while (p != NULL)
    {
        int s = ftell(fp);
        if (s != 0)
        {
            fprintf(fp, "%c", '\n');
        }
        fprintf(fp, "%d", p->id);
        fprintf(fp, "%c", '\t');
        fprintf(fp, "%s", p->name);
        fprintf(fp, "%c", '\t');
        fprintf(fp, "%d", p->rank);
        fprintf(fp, "%c", '\t');
        fprintf(fp, "%s", p->railway_no);
        p = p->next;
    }
    fclose(fp);
    printf("\n\t保存成功！");
    system("pause");
    system("cls");
    return 0;
}

int chengkemanage()
{
    char c;
    system("cls");
    while (1)
    {
        printf("\n\t\t   乘客管理菜单   \n");
        printf("*****************************************\n");
        printf("\t\t1.查 找 乘  客 信 息\n");
        printf("\t\t2.保 存 乘  客 信 息\n");
        printf("\t\t3.返  回  主  菜  单\n");
        printf("*****************************************\n");
        printf("\t请选择您要的服务：\n");
        c = getch();
        switch (c)
        {
        case '1':
            checkchengke();
            _getch();
            system("cls");
            break;
        case '2':
            savechengke();
            break;
        case '3':
            return 0;
        default:
            printf("Please enter a right option number!!\n");
            break;
        }
    }
}
